<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\admin\controller\api;
use lib\Tree;
use app\common\controller\Api;

class Library extends Api {
	
	/**
	 * 字典列表
	 * @return Json
	 */
	public function libraryList(){
		$model = model('Library');
		Tree::instance()->init(collection($model->order('sysCode,orderNumber')->select())->toArray());
		$libraryList = Tree::instance()->getLibraryList(Tree::instance()->getLibraryChild(0, 0));
		$count = count($libraryList);
		return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$libraryList]);
	}
	
	/**
	 * 查询字典
	 * @param int $sysCode
	 * @return Json
	 */
	public function search($sysCode){
		$model = model('Library');
		if($sysCode){
			$libraryList = $model->where('sysCode',$sysCode)->whereOr('parentCode',$sysCode)->order('sysCode,orderNumber')->select();
		}else{
			$libraryList = $model->order('sysCode','orderNumber')->select();
		}
		Tree::instance()->init(collection($libraryList)->toArray());
		$libraryList = Tree::instance()->getLibraryList(Tree::instance()->getLibraryChild($libraryList[0]['parentCode'], $libraryList[0]['parentSubsystemNo']));
		$count = count($libraryList);
		return json(['code'=>1,'msg'=>'用户信息','count'=>$count,'data'=>$libraryList]);
	}
	
	/**
	 * 新增字典
	 * @param array $data
	 * @return
	 */
	public function add($data){
		$model = model('Library');
		if (empty($data)) {
			return json(['code'=>0,'msg'=>'数据异常,请检查']);
		}
		$result = $model->validate('Library')->save($data);
		if (false != $result) {
			setlog('Libraryadd','Library',session('user_auth.id'));
			return json(['code'=>1,'msg'=>'字典添加成功']);
		} else {
			return json(['code'=>0,'msg'=>$model->getError()]);
		}
	}
	
	public function edit($data = null,$libraryId = null){

		if (empty($libraryId)) {
			return json(['code'=>0,'msg'=>'字典项不存在,请检查']);
		}
		
		$model = model('Library');
	  	if ($model->change($data)) {
	  setlog('Libraryupdate','Library',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'字典修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'字典修改失败,请检查']);
        }
	}
	
  /**
   *  删除字典
   * @param  string libraryId 字典id,
   * @return  json
   *
   */
     
    public function del($libraryId) {
       
    	$model = model('Library');
        if ( $libraryId == null ||empty(($model->where(array('id'=>$libraryId))->find()))) {
           return  json(['code'=>0,'msg'=>'字典不存在,请检查']);
         }
          
        if($model->destroy(['id'=>$libraryId]) !== FALSE){
         setlog('Librarydel','Library',session('user_auth.id'));
         return json(['code'=>1,'msg'=>'字典删除成功']);
       }else{
          return $this->json(['code'=>0,'msg'=>'字典删除失败,请检查']);
       }
    }
 	
	/**
	 * 字典项目
	 * @return \think\response\Json
	 */
	public function libraryItemList(){
		$model = model('Library');
		$libraryItemList = $model->field('sysCode,sysCodeName,parentCode')->group('sysCode')->select();
 		$count = count($libraryItemList);
 		return json(['code'=>1,'msg'=>'字典项目','count'=>$count,'data'=>$libraryItemList]);
	}
}